package mobile.TheOldRepublic;

import mobile.TheOldRepublic.YouTubeRssHandler.YouTubeRssItem;
import mobile.TheOldRepublic.util.Constants;
import mobile.TheOldRepublic.video.VideoPlayer;

import com.explodingzombie.util.RssItem;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.ListView;

public class MediaView extends FeedView {
	
	@Override
    protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		loadTitle("Recent Media");
		feedUrl = getResources().getString(R.string.media_youtube);
		handler = new YouTubeRssHandler(this);
//		surfaceView = (SurfaceView)findViewById(R.id.video_sv);
		
//		Toast.makeText(this, DOWNLOAD_WARNING, Toast.LENGTH_LONG).show();
	}

	@Override 
	protected void onListItemClick(ListView l, View v, int position, long id)
	{
		final Context activity = this;
//		super.onListItemClick(l, v, position, id);
		RssItem item = (RssItem)getListAdapter().getItem(position);
		
		try{
			Log.d(getClass().getSimpleName(), "Using YouTube App");
			Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(((YouTubeRssItem)item).getHqUrl()));
			activity.startActivity(myIntent);
		}catch(Exception e){
			e.printStackTrace();
			// Internal video player works, but only low quality videos are available from YouTube. 20110422LP
			Log.d(getClass().getSimpleName(), "Using Internal Video Viewer");
			Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(item.getUrl()));
	        activity.startActivity(myIntent);
		}
	}

	@Override 
	public void onDestroy(){
		super.onDestroy();
	}
	

    @Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		final Context context = this;
		
		menu.clear();
				
		MenuItem about = menu.add("About Videos").setIcon(android.R.drawable.ic_menu_info_details);

		about.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			@Override
			public boolean onMenuItemClick(MenuItem item) {
				setContentView(R.layout.about_video);

				return true;
			}

		});
		return true;
	}
	
	/*@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data){
		if(requestCode == VIDEO_APP){
			if(resultCode == RESULT_OK){
//				startActivity(new Intent(Intent.ACTION_VIEW, data));
				startActivity(data);
			}else if(resultCode == RESULT_CANCELED){
				Toast.makeText(this, "Unable to start video.", Toast.LENGTH_LONG).show();
			}
		}
	}*/
	
	/*@Override
    public void onCreateContextMenu(ContextMenu menu, View v,
                    ContextMenuInfo menuInfo) {
            super.onCreateContextMenu(menu, v, menuInfo);
            menu.add(0, 1, 0, "Open");
            menu.add(0, 2, 0, "Background Download");
    }
	
	@Override
	public boolean onContextItemSelected(MenuItem menuItem) {
        AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) menuItem.getMenuInfo();
        RssItem item = (RssItem)getListAdapter().getItem(menuInfo.position);
        switch(menuItem.getItemId()){
        	case(1):
        		openVideo(item);
        		break;
        	case(2):
				try{
					// Figure out where we're going to save this file. LP
					String state = Environment.getExternalStorageState();
					File outputDir;
					if(Environment.MEDIA_MOUNTED.equals(state)){
						outputDir = Utils.getAppDirectory();
					}else{
						throw new IOException("External Storage unavailable.");
					}
					
	        		// VideoDownloader runs in the background to download files as they're queue'd up. LP
	        		VideoDownloader vd = (VideoDownloader)VideoDownloader.getInstance();
	        		vd.addFileToDownload(item.getUrl(), outputDir);
	        		vd.start();
					Toast.makeText(this, DOWNLOAD_STARTED, Toast.LENGTH_LONG).show();
				}catch(IOException ioe){
					Toast.makeText(this, EXTERNAL_STORAGE_REQUIRED, Toast.LENGTH_LONG).show();
				}
        		break;
        	default:
        		break;
        }
        
        return super.onContextItemSelected(menuItem);
	}*/
	
}
